﻿/*:
 * @plugindesc （新楓之舞專用）分身戰鬥
 * @author 小c、老饕
 *
 * @param Doppelganger Move Frame
 * @text 分身移動時間
 * @type number
 * @min 1
 * @default 60
 * @desc 分身移向中心分身需要的時間，單位：幀，從中心移回原位需要相同的時間
 *
 * @param Doppelganger Wait Frame
 * @text 分身合併等待時間
 * @type number
 * @min 1
 * @default 10
 * @desc 分身合併後到分身開始散開中間等待的時間，單位：幀
 *
 * @param Doppelganger Change Master Code
 * @text 分身切換額外代碼
 * @type note
 * @default ""
 * @desc 真身切換時指定執行的額外內容，這裡this指$gameTroop
 *
 * @param For All Useless
 * @text 群攻無效
 * @type boolean
 * @on 群攻無效
 * @off 群攻有效
 * @default true
 * 
 * @help 
 * 新楓之舞專用，分身忍者戰鬥機制。
 * 需要YEP戰鬥核心支持
 * 需要將它放在CP_SWDA_BaseData的下面
 *
 * 使用方法：分身忍者戰鬥，數據庫需要3個同樣的敵人
 *
 * 敵群事件，第一頁事件註釋【不管第一頁事件是否執行都是有效的】：
 * 分身定義：
 * <Enemy Doppelganger List: x, x, x>
 * 指定所有分身敵人的序號，x≥1
 * 例：<Enemy Doppelganger List: 1, 2, 3>
 * 指定1 2 3號敵人為一組分身，這裡1號敵人指$gameTroop.members()[0]，以此類
 * 推
 * 中心定義：
 * <Enemy Doppelganger Center: x>
 * 指定分身的中心敵人為x，x必須是分身之一
 * 分身中心敵人：分身切換時執行分身合併和分身散開動畫的中心對象
 * 戰鬥開始時只會顯示中心敵人
 * 例：<Enemy Doppelganger Center: 2>
 * 設定第二個敵人為中心
 *
 * 分身切換：以下JS腳本將執行分身切換動畫並【隨機】切換真身
 * $gameTroop.startChangeDoppelgangerMaster()
 * 執行分身切換時戰鬥會暫時阻塞直到所有動畫播放完畢
 * 分身切換後會執行一個響應代碼，內容由插件參數決定
 *
 * 敵人AI指定時可以用以下函數判定是否為真身
 * 敵人的成員函數有如下幾個：
 * enemy.isDoppelganger() 是否為分身之一【包括真身和分身都是true】
 * 非分身戰鬥中其他敵人調用會返回false
 * enemy.isDoppelgangerMaster() 是否為分身真身，非分身戰鬥中不要調用
 * enemy.masterDoppelganger() 返回戰鬥中當前的真身，非分身戰鬥中返回null
 * enemy.isDoppelgangerCenter() 是否為中心對象，非分身戰鬥中返回false
 * enemy.isInvincibleByDoppelganger() 是否為分身假身，分身假身無敵
 * 非分身戰鬥中返回false，不要調用
 * enemy.isDoppelgangerCenter() 是否為中心敵人，非分身戰鬥返回false
 *
 * Game_Troop類相關函數：
 * $gameTroop.doppelgangerEnemies() 所有分身敵人
 * 注意：返回數組長度如果小於2，hasDoppelgangerEnemy返回false
 * $gameTroop.hasDoppelgangerEnemy() 戰鬥是否存在分身敵人
 * $gameTroop.masterDoppelgangerEnemy() 返回當前真身
 * $gameTroop.centerDoppelgangerEnemy() 返回中心敵人對象
 *
 * 實際配置敵群可如下操作：
 * 1. 添加若干相同的敵人作為分身敵人
 * 2. 需要的話此時可進入戰鬥測試確定每個敵人的實際位置
 * 3. 配置分身的兩個標籤
 * 4. 第0回合必須執行一次分身切換
 * 5. 分身切換的執行可以通過事件腳本或者技能效果
 * (1) 事件腳本的執行是有規律的，因此執行條件一般是回合數【0+nX】
 * (2) 如果是技能效果，可以通過戰鬥序列的Eval指令實現，執行條件參考(1)
*/

var Imported = Imported || {};
Imported.CP_Doppelganger = true;

var CP = CP || {};
CP.Doppelganger = CP.Doppelganger || {};
CP.Doppelganger.Params = PluginManager.parameters("CP_Doppelganger");
CP.Doppelganger.DOPPELGANGER_MOVE_FRAME = Number(CP.Doppelganger.Params["Doppelganger Move Frame"]) || 0;
CP.Doppelganger.DOPPELGANGER_WAIT_FRAME = Number(CP.Doppelganger.Params["Doppelganger Wait Frame"]) || 0;
CP.Doppelganger.DOPPELGANGER_CHANGE_MASTER_CODE = new Function(JSON.parse(CP.Doppelganger.Params["Doppelganger Change Master Code"]));
CP.Doppelganger.ITEM_FOR_ALL_USELESS = eval(CP.Doppelganger.Params["For All Useless"]) || false;

//-----------------------------------------
// Game_Troop
//-----------------------------------------
CP.Doppelganger.SETUP_TROOP = Game_Troop.prototype.setup;
Game_Troop.prototype.setup = function(troopId){
	this._doppelgangerEnemies = null;
	CP.Doppelganger.SETUP_TROOP.call(this, troopId);
	this.loadDoppelgangerEnemies();
	this.initDoppelgangerMaster();
};

Game_Troop.prototype.loadDoppelgangerEnemies = function(){
	var list = this.troop().pages[0].list;
	var reg1 = /<Enemy Doppelganger List: ([\S\s]+)>/;
	var reg2 = /<Enemy Doppelganger Center: (\d+)>/;

	this._doppelgangerEnemies = []; //分身的敵人數組，當數組長度至少為2時，此戰鬥才是分身戰鬥
	this._doppelgangerCenterEnemy = 0; //分身中心敵人，當分身交換時，所有分身敵人向中心靠攏然後散開回原位

	list.forEach(function(ev){
		if([108, 408].contains(ev.code)){
			if(reg1.exec(ev.parameters[0])){
				var doppelgangerList = RegExp.$1.trim().split(",").map(function(chars){
					return Number(chars) || 0;
				});
				for(var i = 0; i < doppelgangerList.length; i++){
					if(doppelgangerList[i] > 0)
						this._doppelgangerEnemies.push(doppelgangerList[i]);
				}
			}

			if(reg2.exec(ev.parameters[0]))
				this._doppelgangerCenterEnemy = Number(RegExp.$1) || 0;
		}
	}, this);
};

//所有的分身敵人【沒有考慮主目標問題】
Game_Troop.prototype.doppelgangerEnemies = function(){
	if(!this._doppelgangerEnemies)
		return [];
	var results = [];
	var members = this.members();
	for(var i = 0; i < this._doppelgangerEnemies.length; i++){
		var index = this._doppelgangerEnemies[i] - 1;
		results.push(members[index]);
	}
	return results;
};

Game_Troop.prototype.hasDoppelgangerEnemy = function(){
	return this.doppelgangerEnemies().length > 1;
};

//分身主體對象
Game_Troop.prototype.masterDoppelgangerEnemy = function(){
	if(this.hasDoppelgangerEnemy()){
		var enemies = this.doppelgangerEnemies();
		for(var i = 0; i < enemies.length; i++){
			var enemy = enemies[i];
			if(enemy.isDoppelgangerMaster())
				return enemy;
		}
		return null;
	}else
		return null;
};

//初始化分身主體
Game_Troop.prototype.initDoppelgangerMaster = function(){
	var enemies = this.doppelgangerEnemies();
	if(enemies.length > 0){
		var enemy = enemies[Math.floor(Math.random() * enemies.length)];
		if(!!enemy)
			enemy.setDoppelgangerMaster(true);
	}
};

//隨機切換主體
Game_Troop.prototype.changeDoppelgangerMaster = function(){
	var enemies = this.doppelgangerEnemies();
	var data = this.getDoppelgangerMasterData();
	var newMasterIndex = Math.floor(Math.random() * enemies.length);
	for(var i = 0; i < enemies.length; i++){
		var enemy = enemies[i];
		if(i === newMasterIndex){
			enemy.setDoppelgangerMaster(true);
			enemy.resetDoppelgangerData(data);
		}else
			enemy.setDoppelgangerMaster(false);
	}

};

Game_Troop.prototype.getDoppelgangerMasterData = function(){
	var data = {states: [], stateTurns: {}, statesFloor: [], classId: -1};
	var master = this.masterDoppelgangerEnemy();
	if(!!master){
		data.states = master.statesData();
		data.stateTurns = master.stateTurnsData();
		data.statesFloor = master.statesFloorData();
		data.classId = master.currentClass().id;
	}
	return data;
};

Game_Troop.prototype.centerDoppelgangerEnemy = function(){
	if(this.hasDoppelgangerEnemy()){
		var enemy = this.members()[this._doppelgangerCenterEnemy - 1];
		if(!!enemy)
			return enemy;
		else
			return null;
	}else
		return null;
};

//開始執行主體切換，包括移動到中心、切換主體、移動分散
Game_Troop.prototype.startChangeDoppelgangerMaster = function(){
	var centerDoppelgangerEnemy = this.centerDoppelgangerEnemy();
	var doppelgangerEnemies = this.doppelgangerEnemies();
	if(!!centerDoppelgangerEnemy){
		var centerSprite = centerDoppelgangerEnemy.battler();
		for(var i = 0; i < doppelgangerEnemies.length; i++){
			var enemy = doppelgangerEnemies[i];
			enemy.battler().startToDoppelgangerCenterEnemy(centerSprite
				, CP.Doppelganger.DOPPELGANGER_MOVE_FRAME);
		}
	}
	this.changeDoppelgangerMaster();
	this.onDoppelgangerMasterChanged();
};

Game_Troop.prototype.onDoppelgangerMasterChanged = function(){
	try{
		CP.Doppelganger.DOPPELGANGER_CHANGE_MASTER_CODE.call(this);
	}catch(err){
		console.error(err);
	}
};

CP.Doppelganger.TROOP_ON_BATTLE_END = Game_Troop.prototype.onBattleEnd;
Game_Troop.prototype.onBattleEnd = function(){
	CP.Doppelganger.TROOP_ON_BATTLE_END.call(this);
	this._doppelgangerEnemies = null;
};

//-----------------------------------------
// Game_BattlerBase
//-----------------------------------------
//分身假身
Game_BattlerBase.prototype.isInvincibleByDoppelganger = function(){
	return false;
};

//無敵數據相關
//無敵的屬性效果
CP.Doppelganger.BATTLER_PARAM = Game_BattlerBase.prototype.param;
Game_BattlerBase.prototype.param = function(paramId){
	if(this.isInvincibleByDoppelganger()){
		var master = this.masterDoppelganger();
		if(!!master)
			return CP.Doppelganger.BATTLER_PARAM.call(master, paramId);
		else
			return CP.Doppelganger.BATTLER_PARAM.call(this, paramId);
	}else
		return CP.Doppelganger.BATTLER_PARAM.call(this, paramId);
};

CP.Doppelganger.BATTLER_XPARAM = Game_BattlerBase.prototype.xparam;
Game_BattlerBase.prototype.xparam = function(xparamId){
	if(this.isInvincibleByDoppelganger()){
		var master = this.masterDoppelganger();
		if(!!master)
			return CP.Doppelganger.BATTLER_XPARAM.call(master, xparamId);
		else
			return CP.Doppelganger.BATTLER_XPARAM.call(this, xparamId);
	}else
		return CP.Doppelganger.BATTLER_XPARAM.call(this, xparamId);
};

CP.Doppelganger.BATTLER_SPARAM = Game_BattlerBase.prototype.sparam;
Game_BattlerBase.prototype.sparam = function(sparamId){
	if(this.isInvincibleByDoppelganger()){
		var master = this.masterDoppelganger();
		if(!!master)
			return CP.Doppelganger.BATTLER_SPARAM.call(master, sparamId);
		else
			return CP.Doppelganger.BATTLER_SPARAM.call(this, sparamId);
	}else
		return CP.Doppelganger.BATTLER_SPARAM.call(this, sparamId);
};

//無敵的狀態效果
CP.Doppelganger.BATTLER_STATES = Game_BattlerBase.prototype.states;
Game_BattlerBase.prototype.states = function(){
	if(this.isInvincibleByDoppelganger()){
		var master = this.masterDoppelganger();
		if(!!master)
			return CP.Doppelganger.BATTLER_STATES.call(master);
		else
			return CP.Doppelganger.BATTLER_STATES.call(this);
	}else
		return CP.Doppelganger.BATTLER_STATES.call(this);
};

CP.Doppelganger.BATTLER_STATE_AFFECTED = Game_BattlerBase.prototype.isStateAffected;
Game_BattlerBase.prototype.isStateAffected = function(stateId){
	if(this.isInvincibleByDoppelganger()){
		var master = this.masterDoppelganger();
		if(!!master)
			return CP.Doppelganger.BATTLER_STATE_AFFECTED.call(master, stateId);
		else
			return CP.Doppelganger.BATTLER_STATE_AFFECTED.call(this, stateId);
	}else
		return CP.Doppelganger.BATTLER_STATE_AFFECTED.call(this, stateId);
};

CP.Doppelganger.BATTLER_STATE_FLOOR = Game_BattlerBase.prototype.stateFloor;
Game_BattlerBase.prototype.stateFloor = function(stateId){
	if(this.isInvincibleByDoppelganger()){
		var master = this.masterDoppelganger();
		if(!!master)
			return CP.Doppelganger.BATTLER_STATE_FLOOR.call(master, stateId);
		else
			return CP.Doppelganger.BATTLER_STATE_FLOOR.call(this, stateId);
	}else
		return CP.Doppelganger.BATTLER_STATE_FLOOR.call(this, stateId);
};

//-----------------------------------------
// Game_Battler
//-----------------------------------------
//無敵數據相關
//無敵的屬性效果
CP.Doppelganger.BATTLER_GAIN_HP = Game_Battler.prototype.gainHp;
Game_Battler.prototype.gainHp = function(value){
	if(this.isInvincibleByDoppelganger())
		CP.Doppelganger.BATTLER_GAIN_HP.call(this, 0);
	else
		CP.Doppelganger.BATTLER_GAIN_HP.call(this, value);
};

CP.Doppelganger.BATTLER_GAIN_MP = Game_Battler.prototype.gainMp;
Game_Battler.prototype.gainMp = function(value){
	if(this.isInvincibleByDoppelganger())
		CP.Doppelganger.BATTLER_GAIN_MP.call(this, 0);
	else
		CP.Doppelganger.BATTLER_GAIN_MP.call(this, value);
};

CP.Doppelganger.BATTLER_GAIN_TP = Game_Battler.prototype.gainTp;
Game_Battler.prototype.gainTp = function(value){
	if(this.isInvincibleByDoppelganger())
		CP.Doppelganger.BATTLER_GAIN_TP.call(this, 0);
	else
		CP.Doppelganger.BATTLER_GAIN_TP.call(this, value);
};

CP.Doppelganger.BATTLER_GAIN_SILENT_TP = Game_Battler.prototype.gainSilentTp;
Game_Battler.prototype.gainSilentTp = function(value){
	if(this.isInvincibleByDoppelganger())
		CP.Doppelganger.BATTLER_GAIN_SILENT_TP.call(this, 0);
	else
		CP.Doppelganger.BATTLER_GAIN_SILENT_TP.call(this, value);
};

//無敵的狀態效果
//除了死亡，分身免疫所有狀態
CP.Doppelganger.BATTLER_STATE_ADDABLE = Game_Battler.prototype.isStateAddable;
Game_Battler.prototype.isStateAddable = function(stateId){
	if(this.isInvincibleByDoppelganger())
		return CP.Doppelganger.BATTLER_STATE_ADDABLE.call(this, stateId) 
			&& stateId === this.deathStateId();
	else
		return CP.Doppelganger.BATTLER_STATE_ADDABLE.call(this, stateId);
};

CP.Doppelganger.BATTLER_ADD_BUFF = Game_Battler.prototype.addBuff;
Game_Battler.prototype.addBuff = function(paramId, turns){
	if(!this.isInvincibleByDoppelganger())
		CP.Doppelganger.BATTLER_ADD_BUFF.call(this, paramId, turns);
};

CP.Doppelganger.BATTLER_ADD_DEBUFF = Game_Battler.prototype.addDebuff;
Game_Battler.prototype.addDebuff = function(paramId, turns){
	if(!this.isInvincibleByDoppelganger())
		CP.Doppelganger.BATTLER_ADD_DEBUFF.call(this, paramId, turns);
};

//-----------------------------------------
// Game_Enemy
//-----------------------------------------
CP.Doppelganger.ENEMY_INIT_MEMBERS = Game_Enemy.prototype.initMembers;
Game_Enemy.prototype.initMembers = function(){
	CP.Doppelganger.ENEMY_INIT_MEMBERS.call(this);
	this._doppelgangerMaster = false; //分身主體標記，只有分身主體才會受到傷害
};

Game_Enemy.prototype.statesData = function(){
	return this._states;
};

Game_Enemy.prototype.stateTurnsData = function(){
	return this._stateTurns;
};

Game_Enemy.prototype.statesFloorData = function(){
	return this._statesFloor;
};

Game_Enemy.prototype.resetDoppelgangerData = function(data){
	this._states = data.states;
	this._stateTurns = data.stateTurns;
	this._statesFloor = data.statesFloor;
	if(data.classId > 0)
		this._classId = data.classId;
	this.refresh();
};

//該敵人是否為分身對象：即是否為定義的分身目標之一
Game_Enemy.prototype.isDoppelganger = function(){
	return $gameTroop.doppelgangerEnemies().contains(this);
};

//設定是否為分身主體
Game_Enemy.prototype.setDoppelgangerMaster = function(value){
	this._doppelgangerMaster = !!value;
};

Game_Enemy.prototype.isDoppelgangerMaster = function(){
	return !!this._doppelgangerMaster;
};

//分身主體對象
Game_Enemy.prototype.masterDoppelganger = function(){
	if(this.isDoppelganger() && this.isDoppelgangerMaster())
		return this;
	else
		return $gameTroop.masterDoppelgangerEnemy();
};

//是否因分身而無敵：非主體的分身敵人是無敵的，受傷永遠為0，免疫除死亡以外的所有狀態
//另：帶有這個效果的敵人，所有屬性與分身主體完全一致，因此計算屬性時也可調用該函數
Game_Enemy.prototype.isInvincibleByDoppelganger = function(){
	return this.isDoppelganger() && !this.isDoppelgangerMaster();
};

//中心敵人：分身從這個敵人這裡散開
Game_Enemy.prototype.isDoppelgangerCenter = function(){
	return this.isDoppelganger() && this === $gameTroop.centerDoppelgangerEnemy();
};

CP.Doppelganger.ENEMY_CURRENT_CLASS = Game_Enemy.prototype.currentClass;
Game_Enemy.prototype.currentClass = function(){
	if(this.isInvincibleByDoppelganger()){
		var master = this.masterDoppelganger();
		if(!!master)
			return CP.Doppelganger.ENEMY_CURRENT_CLASS.call(master);
		else
			return CP.Doppelganger.ENEMY_CURRENT_CLASS.call(this);
	}else
		return CP.Doppelganger.ENEMY_CURRENT_CLASS.call(this);
};

//數據（血藍等）同步刷新機制
Game_Enemy.prototype.refresh = function(){
	if(!this.isDoppelganger()) //不是分身對象，正常刷新 補充：如果存在其他的插件寫了enemy.refresh，這裡改成自調用
		Game_Battler.prototype.refresh.call(this);
	else if(this.isDoppelgangerMaster()){
		//分身主體，需要觸發自身和所有分身的刷新
		//分身狀態會負責所有分身對象的刷新，因此非主體對象不需要在這裡調用刷新【else if下面沒有else】
		//分身對象刷新使用refreshDoppelganger函數
		var doppelgangerEnemies = $gameTroop.doppelgangerEnemies();
		for(var i = 0; i < doppelgangerEnemies.length; i++){
			var e = doppelgangerEnemies[i];
			e.refreshDoppelganger();
		}
	}
};

Game_Enemy.prototype.refreshDoppelganger = function(){
	if(!this.isDoppelgangerMaster())
		this.refreshDoppelgangerData();
	//補充：如果存在其他的插件寫了enemy.refresh，這裡改成自調用
	Game_Battler.prototype.refresh.call(this);
};

Game_Enemy.prototype.refreshDoppelgangerData = function(){
	if(this.isInvincibleByDoppelganger()){
		var master = this.masterDoppelganger();
		if(!!master)
			this.setDoppelgangerData(master);
	}
};

Game_Enemy.prototype.setDoppelgangerData = function(master){
	this._hp = master.hp;
	this._mp = master.mp;
	this._tp = master.tp;
	if(!(this.isATBCharging() || this.masterDoppelganger().isATBCharging()))
		this._atbSpeed = master._atbSpeed;	//SWDA：同步ATB
	if(this._hp <= 0 && master.isDead())
		this.performCollapse();
};

//---------------------------------------
// Game_Action
//---------------------------------------
//群攻無效對傷害的影響
CP.Doppelganger.ACTION_MAKE_DAMAGE_VALUE = Game_Action.prototype.makeDamageValue;
Game_Action.prototype.makeDamageValue = function(target, critical){
	if(CP.Doppelganger.ITEM_FOR_ALL_USELESS && this.isForAll() && target.isEnemy() && target.isDoppelganger() 
			&& target.isDoppelgangerMaster())
		return 0; //群攻對分身敵人 當群攻無效時返回0
	else
		return CP.Doppelganger.ACTION_MAKE_DAMAGE_VALUE.call(this, target, critical);
};

//群攻無效對額外效果的影響
CP.Doppelganger.ACTION_APPLY_ITEM_EFFECT = Game_Action.prototype.applyItemEffect;
Game_Action.prototype.applyItemEffect = function(target, effect){
	if(!CP.Doppelganger.ITEM_FOR_ALL_USELESS || !this.isForAll() 
			|| !target.isEnemy() || !target.isDoppelganger())
		CP.Doppelganger.ACTION_APPLY_ITEM_EFFECT.call(this, target, effect);
};

//---------------------------------------
// Scene_Battle
//---------------------------------------
//當敵人分身在移動動畫的時候，阻塞戰鬥
CP.Doppelganger.SCENE_BATTLE_IS_BUSY = Scene_Battle.prototype.isBusy;
Scene_Battle.prototype.isBusy = function(){
	if(this._spriteset.hasDoppelgangerMoving())
		return true;
	return CP.Doppelganger.SCENE_BATTLE_IS_BUSY.call(this);
};

//---------------------------------------
// Spriteset_Battle
//---------------------------------------
Spriteset_Battle.prototype.enemySprites = function() {
    return this._enemySprites;
};

Spriteset_Battle.prototype.hasDoppelgangerMoving = function(){
	var sprites = this.enemySprites();
	for(var i = 0; i < sprites.length; i++){
		if(sprites[i].isDoppelgangerMoving())
			return true;
	}
	return false;
};

//---------------------------------------
// Sprite_Enemy
//---------------------------------------
CP.Doppelganger.SPRITE_ENEMY_INIT_MEMBERS = Sprite_Enemy.prototype.initMembers;
Sprite_Enemy.prototype.initMembers = function(){
	this._moveDoppelgangerStart = false; //分身收移動標記
	this._moveDoppelgangerEnd = false; //分身散移動標記
	this._moveDoppelgangerFrame = 0; //分身移動持續時間【幀】
	this._doppelgangerVisible = false; //開場分身必須隱藏
	this._doppelgangerWaitFrame = -1; //分身合併-分散中間的等待幀
	CP.Doppelganger.SPRITE_ENEMY_INIT_MEMBERS.call(this);
};

Sprite_Enemy.prototype.setDoppelgangerWaitFrame = function(frame){
	if(frame > 0)
		this._doppelgangerWaitFrame = frame;
};

Sprite_Enemy.prototype.homeX = function(){
	return this._homeX;
};

Sprite_Enemy.prototype.homeY = function(){
	return this._homeY;
};

Sprite_Enemy.prototype.startToDoppelgangerCenterEnemy = function(centerEnemySprite, frame){
	if(this === centerEnemySprite)
		return;
	var offsetX = centerEnemySprite.homeX() - this._homeX;
	var offsetY = centerEnemySprite.homeY() - this._homeY;

	this._moveDoppelgangerStart = true;
	this._moveDoppelgangerFrame = frame;
	//當分身目標不是中心而不可見時，移動到中心的時間減半
	//因為這個時候這些移動目標是不可見的，不需要太多時間
	if(!this._doppelgangerVisible)
		frame = Math.floor(frame / 2);
	this.startMove(offsetX, offsetY, frame);
};

CP.Doppelganger.SPRITE_ENEMY_ON_MOVE_END = Sprite_Enemy.prototype.onMoveEnd;
Sprite_Enemy.prototype.onMoveEnd = function(){
	CP.Doppelganger.SPRITE_ENEMY_ON_MOVE_END.call(this);
	this._doppelgangerVisible = true;
	if(this._moveDoppelgangerStart && this._moveDoppelgangerFrame > 0)
		this.setDoppelgangerWaitFrame(CP.Doppelganger.DOPPELGANGER_WAIT_FRAME);
	else if(this._moveDoppelgangerEnd)
		this._moveDoppelgangerEnd = false;
};

CP.Doppelganger.SPRITE_ENEMY_UPDATE = Sprite_Enemy.prototype.update;
Sprite_Enemy.prototype.update = function(){
	CP.Doppelganger.SPRITE_ENEMY_UPDATE.call(this);
	this.updateDoppelgangerWaitCount(); //有粉散則執行分散
};

CP.Doppelganger.SPRITE_ENEMY_UPDATE_VISIBILITY = Sprite_Enemy.prototype.updateVisibility;
Sprite_Enemy.prototype.updateVisibility = function(){
	if(!!this._enemy && this._enemy.isDoppelgangerCenter())
		this._doppelgangerVisible = true;
	CP.Doppelganger.SPRITE_ENEMY_UPDATE_VISIBILITY.call(this);
	if(!!this._enemy && this._enemy.isDoppelganger() && !this._doppelgangerVisible)
		this.visible = false;
};

Sprite_Enemy.prototype.updateDoppelgangerWaitCount = function(){
	if(this._doppelgangerWaitFrame > 0){
		this._doppelgangerWaitFrame--;
		if(this._doppelgangerWaitFrame <= 0){
			this.startMove(0, 0, this._moveDoppelgangerFrame);
			this._moveDoppelgangerStart = false;
			this._moveDoppelgangerFrame = 0;
			this._moveDoppelgangerEnd = true;
			this._doppelgangerWaitFrame = -1;
		}
	}
};

//分身動畫是否在運行【移動】，如果在運行，戰鬥必須阻塞
Sprite_Enemy.prototype.isDoppelgangerMoving = function(){
	if(!this._enemy)
		return false;
	if(!this._enemy.isDoppelganger())
		return false;
	if(this._moveDoppelgangerStart || this._moveDoppelgangerEnd)
		return true;
	return false;
};